/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * This servlet is called to calculate the precomp
 */
package db;

import Model.Terms;
import com.mysql.jdbc.Connection;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Bryan
 */
public class Fillup extends HttpServlet {
    private String ui;
    private String sc;
    private String nf;
    private String ev;
    private String ds;
    private String cf;
    private String pf;
    private String bc;
    private String lc;
    private String cb;
    private String lg;
    private Terms term = new Terms();
    /**
     * Fills up a default value for all the percentages in the precomputation module
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       java.sql.Connection con = DBConnector.connect();
       
        
        try{
       
        String clienttype = request.getParameter("clienttype");
        String subtype = request.getParameter("subtype");
        String loantype = request.getParameter("loantype");
        String terms = request.getParameter("term");
        String pn = request.getParameter("pn");
        String precompno = request.getParameter("precompno");
            
        float loan= Float.parseFloat(terms)* Float.parseFloat(pn);
            
                PreparedStatement ps = con.prepareStatement
                        ("Select less_ui,service_charge,notary_fee,evat,docs,collectionfee,"
                        + "processingfee,bank_charge,lbc_charge,cashbound from terms where clienttype='"+clienttype+"' and subtype='"+subtype+"' and loantype='"+loantype+"'");
                
                
                    ResultSet rs = ps.executeQuery();
                    
                       if(rs!=null){ 
                           boolean blnGotSomeRows = false;  
                                while(rs.next())
                                {
                                blnGotSomeRows = true;    
                                term.setfLessui(rs.getFloat("less_ui"));
                                term.setfServicecharge(rs.getFloat("service_charge"));
                                term.setfNotaryfee(rs.getFloat("notary_fee"));
                                term.setfEvat(rs.getFloat("evat"));
                                term.setfDocs(rs.getFloat("docs"));
                                term.setfCollectionfee(rs.getFloat("collectionfee"));
                                term.setfProcessingfee(rs.getFloat("processingfee"));
                                term.setfBankcharge(rs.getFloat("bank_charge"));
                                term.setfLbccharge(rs.getFloat("lbc_charge"));
                                term.setfCashbound(rs.getFloat("cashbound"));

                                }
                                
                                if(blnGotSomeRows){
                                    
                                }else{
                                    term.setfLessui(0.0f);
                                    term.setfServicecharge(0.0f);
                                    term.setfNotaryfee(0.0f);
                                    term.setfEvat(0.0f);
                                    term.setfDocs(0.0f);
                                    term.setfCollectionfee(0.0f);
                                    term.setfProcessingfee(0.0f);
                                    term.setfBankcharge(0.0f);
                                    term.setfLbccharge(0.0f);
                                    term.setfCashbound(0.0f);
                                }
                            
                       }
                       
                HttpSession session = request.getSession(true);
                session.setAttribute("terms",term);
                response.sendRedirect("precomputation.jsp?clienttype="+clienttype+"&loantype="+loantype+"&subtype="+subtype+"&terms="+terms+"&pn="+pn+"&loans="+loan+"&precompno="+precompno);
            
        }catch(SQLException e)
        {
            System.out.println(e); 
           
        }
    

    }
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    /**
     * @return the ui
     */
    public String getUi() {
        return ui;
    }

    /**
     * @param ui the ui to set
     */
    public void setUi(String ui) {
        this.ui = ui;
    }

    /**
     * @return the sc
     */
    public String getSc() {
        return sc;
    }

    /**
     * @param sc the sc to set
     */
    public void setSc(String sc) {
        this.sc = sc;
    }

    /**
     * @return the nf
     */
    public String getNf() {
        return nf;
    }

    /**
     * @param nf the nf to set
     */
    public void setNf(String nf) {
        this.nf = nf;
    }

    /**
     * @return the ev
     */
    public String getEv() {
        return ev;
    }

    /**
     * @param ev the ev to set
     */
    public void setEv(String ev) {
        this.ev = ev;
    }

    /**
     * @return the ds
     */
    public String getDs() {
        return ds;
    }

    /**
     * @param ds the ds to set
     */
    public void setDs(String ds) {
        this.ds = ds;
    }

    /**
     * @return the cf
     */
    public String getCf() {
        return cf;
    }

    /**
     * @param cf the cf to set
     */
    public void setCf(String cf) {
        this.cf = cf;
    }

    /**
     * @return the pf
     */
    public String getPf() {
        return pf;
    }

    /**
     * @param pf the pf to set
     */
    public void setPf(String pf) {
        this.pf = pf;
    }

    /**
     * @return the bc
     */
    public String getBc() {
        return bc;
    }

    /**
     * @param bc the bc to set
     */
    public void setBc(String bc) {
        this.bc = bc;
    }

    /**
     * @return the lc
     */
    public String getLc() {
        return lc;
    }

    /**
     * @param lc the lc to set
     */
    public void setLc(String lc) {
        this.lc = lc;
    }

    /**
     * @return the cb
     */
    public String getCb() {
        return cb;
    }

    /**
     * @param cb the cb to set
     */
    public void setCb(String cb) {
        this.cb = cb;
    }

    /**
     * @return the lg
     */
    public String getLg() {
        return lg;
    }

    /**
     * @param lg the lg to set
     */
    public void setLg(String lg) {
        this.lg = lg;
    }

    /**
     * @return the loantype
     */
   
}
